// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem1052.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=1052 lang=cpp
 *
 * [1052] 爱生气的书店老板
 */

// @lc code=start
class Solution {
public:
    int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {

        int res = 0;
        //1.计算正常情况下的满意顾客数
        for (int i = 0; i < customers.size();i++)
        {
            if(grumpy[i] == 0)
                res += customers[i];
        }

        //2. 滑动窗口进行计算增量
        int temp = 0;
        int left = 0;
        int right = 0;
        int ans = 0;    //存储最大增量
        for (right; right < customers.size() ; right++)
        {
            // 处理右边界数据
            if(grumpy[right] == 1)
            {
                temp += customers[right];
            }

            // 判断是否左移窗口，判断窗口大小是否满足X
            if(right - left + 1 > X)
            {
                //窗口大小不满足，判断左移元素是否需要更改
                if(grumpy[left] == 1)
                {
                    temp -= customers[left];
                }
                left++;
            }

            //更新最大的增量
            ans = max(temp, ans);
        }
        return res + ans;
    }
};
// @lc code=end

